package com.wmh.baseservice.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wmh.baseservice.admin.pojo.AdGoods;
import com.wmh.baseservice.admin.mapper.AdGoodsMapper;
import com.wmh.baseservice.admin.service.AdGoodsService;
import com.wmh.baseservice.common.ex.ServiceException;
import com.wmh.baseservice.common.mybatisplus.methods.CommonServiceImpl;
import com.wmh.baseservice.common.utils.FileUtil;
import com.wmh.baseservice.common.utils.LogUtil;
import com.wmh.baseservice.common.utils.PathUtil;
import com.wmh.baseservice.user.pojo.Goods;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.util.Map;

/**
 * @author wmh generator
 * @since 2021-12-27
 */
@Service
public class AdGoodsServiceImpl extends CommonServiceImpl<AdGoodsMapper, AdGoods> implements AdGoodsService {


    @Resource
    private AdGoodsMapper adGoodsMapper;

    /**
     * 删除商品
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(AdGoods adGoods) {
        if (adGoodsMapper.deleteById(adGoods.getId()) != 1) {
            throw new ServiceException();
        }
        File file = new File(PathUtil.getFullRealPath() + "goods/" + adGoods.getUrlId());
        //删除图片
        if (file.exists()) {
            FileUtil.deleteAllFiles(file);
            if (file.exists()) {
                LogUtil.error("商品图片文件夹删除失败");
            }else {
                LogUtil.info("商品图片文件夹删除成功!");
            }
        } else {
            LogUtil.error("商品图片文件夹不存在");
        }
    }

    /**
     * 用户端商品列表（包含商家和管理员商品）
     */
    @Override
    public IPage<Goods> goodsList(Page<Goods> goodsPage, Map<String, Object> map) {
        return adGoodsMapper.goodsList(goodsPage,map);
    }
}
